import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from unittest import TestCase
from ddt import ddt, data, unpack
import pymysql


@ddt
class TestSign(TestCase):
    add_test_data = [
        ['lisi', '123456', '伊朗中央银行', '借记卡', '1234567891548796'],
        ['lisi', '123456', '伊拉克国民银行', '借记卡', '12345678915487967'],
        ['lisi', '123456', '伊拉克国民银行', '借记卡', '1234567891548796'],
        ['lisi', '123456', '伊拉克国民银行', '信用卡', '1234567891548796']
    ]

    delete_test_data = [
        ['lisi', '123456', 2]
    ]

    edit_test_data = [
        [1, 'lisi', '123456', 1, '啊啊啊', "cardBank"],
        [2, 'lisi', '123456', 2, '1111111111111111', "cardNum"],
        [3, 'lisi', '123456', 2, '22222222222222222', "cardNum"],
        [4, 'lisi', '123456', 3, 1, "type"]
    ]

    @data(*add_test_data)
    @unpack
    def testAddCard(self, username, password, bankname, cardtype, cardnum):
        con = pymysql.connect(host='localhost', port=3306, user='root', password='root', database='finance')
        cursor = con.cursor()
        sql = 'select cardNum from bankcard'
        cursor.execute(sql)
        con.commit()
        beforeAdd = cursor.fetchall()
        # option = webdriver.ChromeOptions()
        # option.add_argument('--headless')
        # 创建谷歌浏览器对象
        driver = webdriver.Chrome()

        # 窗口最大化
        driver.maximize_window()

        driver.get("http://localhost:90")

        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
        # time.sleep(1)
        # 点击登录按钮
        driver.find_element(By.XPATH, '//button[@id="login_btn"]').click()
        time.sleep(1)
        # 点击个人中心
        driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[4]/a").click()
        time.sleep(1)
        # 点击银行卡管理
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]').click()
        # 点击新增按钮
        driver.find_element(By.XPATH, '//button[@id="bankCard_add_modal_btn"]').click()
        time.sleep(1)
        # 输入银行名称
        driver.find_element(By.XPATH, '//input[@id="cardBank_add_input"]').send_keys(bankname)
        # 选择银行卡类型
        if cardtype == '借记卡':
            driver.find_element(By.XPATH, '//*[@id="bankCardAddModal"]/div/div/div[2]/form/div[2]/div[1]/div/label[1]').click()
        else:
            driver.find_element(By.XPATH, '//*[@id="bankCardAddModal"]/div/div/div[2]/form/div[2]/div[1]/div/label[2]').click()
        # 输入卡号
        driver.find_element(By.XPATH, '//input[@id="cardNum_add_input"]').send_keys(cardnum)
        # 点击保存按钮
        driver.find_element(By.XPATH, '//*[@id="bankCard_save_btn"]').click()
        time.sleep(1)

        # 获取数据库中所有的卡号
        cursor.execute(sql)
        con.commit()
        afterAdd = cursor.fetchall()
        con.close()
        cursor.close()

        # 退出浏览器
        driver.quit()

        if len(afterAdd) > len(beforeAdd):
            self.assertIn((cardnum,), afterAdd)
        else:
            self.assertTrue(False)

    @data(*delete_test_data)
    @unpack
    def testDeleteCard(self, username, password, flag):
        con = pymysql.connect(host='localhost', port=3306, user='root', password='root', database='finance')
        cursor = con.cursor()
        beforesql = 'select id from bankcard where cardBank = "伊拉克国民银行" and cardNum = "12345678915487967" and type = "1"'
        cursor.execute(beforesql)
        con.commit()
        beforeDelete = cursor.fetchall()
        print(beforeDelete)
        # option = webdriver.ChromeOptions()
        # option.add_argument('--headless')
        # 创建谷歌浏览器对象
        driver = webdriver.Chrome()

        # 窗口最大化
        driver.maximize_window()

        driver.get("http://localhost:90")

        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
        # time.sleep(1)
        # 点击登录按钮
        driver.find_element(By.XPATH, '//button[@id="login_btn"]').click()
        time.sleep(1)
        # 点击个人中心
        driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[4]/a").click()
        time.sleep(1)
        # 点击银行卡管理
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]').click()
        # 选择一张银行卡，点击删除
        driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div[4]/div/div/div[1]/ul/li/button[2]').click()
        time.sleep(1)
        aftersql = 'select id from bankcard'
        cursor.execute(aftersql)
        con.commit()
        afterDelete = cursor.fetchall()
        con.close()
        cursor.close()
        if flag == 1:
            driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
            driver.quit()
            self.assertNotIn(beforeDelete, afterDelete)
        else:
            driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[2]').click()
            driver.quit()
            self.assertIn(beforeDelete[0], afterDelete)

    @data(*edit_test_data)
    @unpack
    def testEdit(self, num, username, password, flag, content, title):
        con = pymysql.connect(host='localhost', port=3306, user='root', password='root', database='finance')
        cursor = con.cursor()
        if num == 1:
            beforesql = f'select id from bankcard where cardBank = "伊拉克国民银行" and cardNum = "2222222222222222" and type = "2"'
        elif num == 2:
            beforesql = f'select id from bankcard where cardBank = "啊啊啊" and cardNum = "2222222222222222" and type = "2"'
        elif num == 3:
            beforesql = f'select id from bankcard where cardBank = "啊啊啊" and cardNum = "1111111111111111" and type = "2"'
        else:
            beforesql = f'select id from bankcard where cardBank = "啊啊啊" and cardNum = "22222222222222222" and type = "2"'
        cursor.execute(beforesql)
        con.commit()
        beforeDelete = cursor.fetchall()
        print(beforeDelete)
        target_id = beforeDelete[0][0]
        # option = webdriver.ChromeOptions()
        # option.add_argument('--headless')
        # 创建谷歌浏览器对象
        driver = webdriver.Chrome()

        # 窗口最大化
        driver.maximize_window()

        driver.get("http://localhost:90")

        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
        # time.sleep(1)
        # 点击登录按钮
        driver.find_element(By.XPATH, '//button[@id="login_btn"]').click()
        time.sleep(1)
        # 点击个人中心
        driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[4]/a").click()
        time.sleep(1)
        # 点击银行卡管理
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]').click()
        time.sleep(1)
        # 选择一张银行卡，点击编辑
        driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div[4]/div/div/div[1]/ul/li/button[1]').click()
        time.sleep(1)
        if flag == 1:
            # 在银行输入框中输入银行名称
            driver.find_element(By.XPATH, '//*[@id="cardBank_update_input"]').clear()
            driver.find_element(By.XPATH, '//*[@id="cardBank_update_input"]').send_keys(content)
        elif flag == 2:
            # 在卡号输入框中输入银行卡号
            driver.find_element(By.XPATH, '//*[@id="cardNum_update_input"]').clear()
            driver.find_element(By.XPATH, '//*[@id="cardNum_update_input"]').send_keys(content)
        else:
            # 更改银行卡类型
            driver.find_element(By.XPATH, '//*[@id="bankCardUpdateModal"]/div/div/div[2]/form/div[2]/div[1]/div/label[1]').click()
        time.sleep(1)
        # 点击更新按钮
        driver.find_element(By.XPATH, '//*[@id="bankCard_update_btn"]').click()
        time.sleep(1)

        # 查找数据库信息，对比是否修改成功
        aftersql = f'select {title} from bankcard where id = {target_id}'
        cursor.execute(aftersql)
        afterDelete = cursor.fetchall()
        cursor.close()
        con.close()
        driver.quit()
        target_card = afterDelete[0][0]
        self.assertEqual(target_card, content)
